package com.ruoyi.workflow.service.impl;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.workflow.mapper.StoreInfoMapper;
import com.ruoyi.workflow.domain.StoreInfo;
import com.ruoyi.workflow.service.IStoreInfoService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 商店信息Service业务层处理
 *
 * @author xiaohao
 * @date 2022-11-25
 */
@Service
public class StoreInfoServiceImpl extends ServiceImpl<StoreInfoMapper, StoreInfo> implements IStoreInfoService {

    @Resource
    private ISysUserService userService;

    /**
     * 查询商店信息列表
     *
     * @param storeInfo 商店信息
     * @return 商店信息
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<StoreInfo> selectStoreInfoList(StoreInfo storeInfo) {
        List<StoreInfo> storeInfos = null;
//        查询所有user数据的结果集
        SysUser user2 = new SysUser();
        List<SysUser> sysUsers = userService.selectUserList(user2);

        //默认是对第一条SQL语句进行分页
        PageUtils.startPage();
//        查询商品表中的结果集
        List<StoreInfo> storeInfoList = getBaseMapper().selectStoreInfoList(null);
        SysUser user1 = null;


        /*for (int i = 0; i < storeInfoList.size(); i++) {
            user1 = sysUsers.stream().filter(user -> user.getUserId() == storeInfoList.get(1).getManagerId()).collect(Collectors.toList()).get(0);
            storeInfoList.get(i).setManagerName(user1.getNickName());//管理员姓名
            storeInfoList.get(i).setManagerTel(user1.getPhonenumber());//管理员电话
            getBaseMapper().updateById(storeInfoList.get(i));
        }*/


        for (StoreInfo info : storeInfoList) {
            //将用户id与商品表中的 用户id进行equals 为true 才进行 set 设置值
            user1 = sysUsers.stream().filter(user -> user.getUserId().equals(info.getManagerId()))
                    .collect(Collectors.toList()).get(0);

            info.setManagerName(user1.getNickName());//管理员姓名
            info.setManagerTel(user1.getPhonenumber());//管理员电话
            getBaseMapper().updateById(info);
        }

        storeInfos = getBaseMapper().selectStoreInfoList(null);

        return storeInfos;
    }

    @Override
    public int deleteByAllId(List<Long> asList) {
        return getBaseMapper().deleteByAllId(asList);
    }


    @Override
    public boolean updateById(StoreInfo entity) {
        SysUser user = userService.selectUserById(entity.getManagerId());
        entity.setManagerName(user.getNickName());//管理员姓名
        entity.setManagerTel(user.getPhonenumber());//管理员电话
        //保存
        return super.updateById(entity);
    }

    @Override
    public boolean save(StoreInfo entity) {
        SysUser user = userService.selectUserById(entity.getManagerId());
        entity.setManagerName(user.getNickName());//管理员姓名
        entity.setManagerTel(user.getPhonenumber());//管理员电话

        return super.save(entity);
    }


}
